package com.xiaoshuidi.cloud.module.report.service.finance;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.oss.core.OosFileService;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.*;
import com.xiaoshuidi.cloud.module.report.convert.finance.OwnerIncomeSummaryConvert;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.OwnerIncomeSummaryReportDO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.report.OwnerIncomeSummaryReportMapper;
import com.xiaoshuidi.cloud.module.report.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.report.framework.security.config.ConfigValues;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.List;


@Service
@RequiredArgsConstructor
@Slf4j
public class OwnerIncomeSummaryReportServiceImpl implements OwnerIncomeSummaryReportService {

    private final OwnerIncomeSummaryReportMapper ownerIncomeSummaryReportMapper;
    private final OosFileService oosFileService;
    private final ConfigValues configValues;


    @Override
    public PageResult<OwnerSumIncomePageRespVO> totalIncomePage(OwnerSumIncomePageReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        long num = ownerIncomeSummaryReportMapper.countListForPage(reqVO);
        List<OwnerIncomeSummaryReportDO> list = ownerIncomeSummaryReportMapper.selectListForPage(reqVO);
        List<OwnerSumIncomePageRespVO> respVOS = OwnerIncomeSummaryConvert.INSTANCE.convertSumIncomeListForPage(list);
        return new PageResult<>(respVOS, num);
    }

    @Override
    public ExportRespVO sumIncomeExport(OwnerSumIncomePageReqVO req) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        req.setTenantId(loginUser.getTenantId());
        String startDate = req.getStartDate();
        String endDate =req.getEndDate();
        String fileName = "";
        if(startDate.equals(endDate)){
            fileName = startDate + "成本汇总表.xlsx";
        }else {
            fileName = startDate+"~"+endDate + "成本汇总表.xlsx";
        }
        String filePath = configValues.getNfsPath() + File.separator +fileName;
        ExcelWriter excelWriter = EasyExcel.write(filePath, SumIncomeExportRespVO.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
        List<OwnerIncomeSummaryReportDO> list = ownerIncomeSummaryReportMapper.selectListForExport(req);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ErrorCodeConstants.OWNER_SUMMARY_INCOME_NOT_EXISTS);
        }
        List<OwnerSumIncomeExportRespVO> respVOS = OwnerIncomeSummaryConvert.INSTANCE.convertSumIncomeListForExport(list);
        excelWriter.write(respVOS, sheet);
        excelWriter.finish();
        //上传到oos返回URL
        String url = oosFileService.oosUploadFile(new File(filePath));
        ExportRespVO resp = new ExportRespVO();
        if(url.startsWith("http:")){
            url = url.replace("http:","https:");
        }
        resp.setUrl(url);
        resp.setFileName(fileName);
        return resp;
    }

    @Override
    public List<OwnerIncomeSummaryReportDO> selectSumByCurrentMonth(String currentMonth) {
        LambdaQueryWrapper<OwnerIncomeSummaryReportDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OwnerIncomeSummaryReportDO::getStatisticsMonth,currentMonth);
        return ownerIncomeSummaryReportMapper.selectList(queryWrapper);
    }

    @Override
    public void batchInsert(List<OwnerIncomeSummaryReportDO> list) {
        ownerIncomeSummaryReportMapper.insertBatch(list);
    }
    @Override
    public void batchUpdate(List<OwnerIncomeSummaryReportDO> list) {
        ownerIncomeSummaryReportMapper.updateBatch(list, list.size());
    }
}
